package com.timedomain.him.remake.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class TimeUtils {
    public static String msToTimeStr(long ms){
        long hour = ms / 3600000;
        StringBuilder sb = new StringBuilder();
        if(hour > 0){
            sb.append(hour).append(":");
        }
        long minute = (ms % 3600000) / 60000;
        if(minute < 10){
            sb.append("0");
        }
        sb.append(minute).append(":");
        long second = (ms % 60000) / 1000;
        if(second < 10){
            sb.append("0");
        }
        return sb.append(second).toString();
    }

    // 每天从4点开始
    public static final int hoursThatBeginOfDay = 4;

    public static final long millisPerDay = 86400 * 1000;

    public static long toDateStamp(long timestamp){
        return timestamp / millisPerDay;
    }

    public static long dateStampToTimeStamp(long datestamp){
        return datestamp * millisPerDay;
    }

    private static long cachedEpoch = -114514;

    private static void calcCacheEpoch(){
        if(cachedEpoch == -114514){
            Calendar cld = Calendar.getInstance();
            cld.set(2000,(11-1),20,hoursThatBeginOfDay,0,0);
            cld.set(Calendar.MILLISECOND,0);
            cachedEpoch = cld.getTimeInMillis();
        }
    }

    public static long getTimeStampIgnoreTimeZone(){
        calcCacheEpoch();
        return System.currentTimeMillis() - cachedEpoch;
    }

    public static long toSystemTimeStamp(long internalTimeStamp){
        calcCacheEpoch();
        return internalTimeStamp + cachedEpoch;
    }

    public static long toEpochedTimeStamp(long systemTimeStamp){
        calcCacheEpoch();
        return systemTimeStamp - cachedEpoch;
    }

    public static long getByDateTime(int year,int month,int day,int hour,int minute,int second){
        Calendar cld = Calendar.getInstance();
        cld.set(year,month-1,day,hour,minute,second);
        return toEpochedTimeStamp(cld.getTimeInMillis());
    }

    public static boolean isWeekend(long internalTimeStamp){
        long systemTimeStamp = toSystemTimeStamp(internalTimeStamp);
        Calendar cld = Calendar.getInstance();
        cld.setTimeInMillis(systemTimeStamp);
        return cld.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY ||  cld.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY ;
    }

    public static int getDayOfWeek(long internalTimeStamp){
        long systemTimeStamp = toSystemTimeStamp(internalTimeStamp);
        Calendar cld = Calendar.getInstance();
        cld.setTimeInMillis(systemTimeStamp);
        int dayOfWeek = cld.get(Calendar.DAY_OF_WEEK) - Calendar.MONDAY ;
        if(dayOfWeek < 0){dayOfWeek += 7;};
        return dayOfWeek;
    }

    public static boolean isSaturnday(long internalTimeStamp){
        long systemTimeStamp = toSystemTimeStamp(internalTimeStamp);
        Calendar cld = Calendar.getInstance();
        cld.setTimeInMillis(systemTimeStamp);
        return cld.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY;
    }

    private static SimpleDateFormat sdfToday = new SimpleDateFormat("HH:mm", Locale.US);
    private static SimpleDateFormat sdfYesterday = new SimpleDateFormat("昨天 HH:mm", Locale.US);
    private static SimpleDateFormat sdfNormal = new SimpleDateFormat("MM月dd日 HH:mm", Locale.US);

    public static String getTimeDesc(long internalTimeStamp){
        long date = toDateStamp(internalTimeStamp);
        long today = toDateStamp(getTimeStampIgnoreTimeZone());
        long sysTime = toSystemTimeStamp(internalTimeStamp);
        if(today <= date){
            return sdfToday.format(new Date(sysTime));
        }
        else if(today - 1 == date){
            return sdfYesterday.format(new Date(sysTime));
        } else{
            return sdfNormal.format(new Date(sysTime));
        }
    }

}
